﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudyCode.Reflection
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// code-behind
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var users = new List<User>();
            for (int i = 0; i < 100; i++)
            {
                users.Add(new User
                {
                    IdNumber = i,
                    Name1 = "Name" + i,
                    Age = 15 + i % 5,
                    Gender = i % 2 == 1 ? true : false,
                    Address = "Address" + i
                });
            }

            //v1。在设计器里面设计

            //v2 在代码里面写
            //var IdColumn = new DataGridViewTextBoxColumn()
            //{
            //    Name="colId",
            //    HeaderText = "身份证号",
            //    DataPropertyName = "IdNumber",
            //};
            //dataGridView1.Columns.Add(IdColumn);


            //var nameColumn = new DataGridViewTextBoxColumn()
            //{
            //    Name = "colName",
            //    HeaderText = "名称",
            //    DataPropertyName = "Name",
            //};
            //dataGridView1.Columns.Add(nameColumn);


            //var ageColumn = new DataGridViewTextBoxColumn()
            //{
            //    Name = "colAge",
            //    HeaderText = "年龄",
            //    DataPropertyName = "Age",
            //};
            //dataGridView1.Columns.Add(ageColumn);

            //v3 去掉代码里面的重复劳动
            //var headTextList = new List<string> { "身份证号", "名称", "年龄" };
            //var dataMappingList = new List<string> { "IdNumber", "Name", "Age" };

            //var columns = new List<DataGridViewTextBoxColumn>();
            //for (int i = 0; i < headTextList.Count; i++)
            //{
            //    var column = new DataGridViewTextBoxColumn()
            //    {
            //        HeaderText = headTextList[i],
            //        DataPropertyName = dataMappingList[i],
            //    };
            //    columns.Add(column);
            //}

            //dataGridView1.Columns.AddRange(columns.ToArray());

            //v3.5 对齐
            //var myColumnList = new List<MyColumn>
            //{
            //    new MyColumn() {Name = "IdNumber", Text = "身份证号"},
            //    new MyColumn() {Name = "Name", Text = "名称"},
            //    new MyColumn() {Name = "Age", Text = "年龄"},
            //};
            //var columns = new List<DataGridViewTextBoxColumn>();
            //for (int i = 0; i < myColumnList.Count; i++)
            //{
            //    var column = new DataGridViewTextBoxColumn()
            //    {
            //        HeaderText = myColumnList[i].Text,
            //        DataPropertyName = myColumnList[i].Name,
            //    };
            //    columns.Add(column);
            //}

            //dataGridView1.Columns.AddRange(columns.ToArray());

            //v4 我们获取元数据中的

            //Type 获取类型的元数据信息
            //PropertyInfo 获取属性的元数据信息
            //var columns = new List<DataGridViewTextBoxColumn>();
            //var userProperties = typeof(User).GetProperties();

            //foreach (var item in userProperties)
            //{
            //    var attrs = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
            //    var desc = (DescriptionAttribute)attrs[0];
            //    var column = new DataGridViewTextBoxColumn()
            //    {
            //        HeaderText = desc.Description,
            //        DataPropertyName = item.Name,
            //    };
            //    columns.Add(column);
            //}
            //dataGridView1.Columns.AddRange(columns.ToArray());

            //v5 方法封装
            //datagridview数据绑定
            //1,生成列
            dataGridView1.Columns.AddRange(BindColumns(typeof(User)));
            //2，绑定数据源
            this.dataGridView1.DataSource = users;
        }

        public class MyColumn
        {
            public string Name { get; set; }

            public string Text { get; set; }
        }

        private DataGridViewColumn[] BindColumns(Type type)
        {
            //Type 获取类型的元数据信息
            //PropertyInfo 获取属性的元数据信息
            return type.GetProperties().Select(item =>
            {
                var attrs = item.GetCustomAttributes(typeof(ColumnAttribute), false);
                var desc = (ColumnAttribute)attrs[0];
                var column = getColumn(desc.Type);
                column.Name = item.Name;
                column.HeaderText = desc.Description;
                column.DataPropertyName = item.Name;
                return column;
            }).ToArray();
        }

        private DataGridViewColumn getColumn(ColumnType type)
        {
            switch (type)
            {
                case ColumnType.CheckBox:
                    return new DataGridViewCheckBoxColumn();
                case ColumnType.TextBox:
                    return new DataGridViewTextBoxColumn();
                case ColumnType.Link:
                    return new DataGridViewLinkColumn();
                default:
                    return new DataGridViewTextBoxColumn();
            }
        }
    }

    public class ColumnAttribute : Attribute
    {
        public string Description { get; set; }

        public ColumnType Type { get; set; }
    }

    public enum ColumnType
    {
        TextBox,
        CheckBox,
        Link,
    }

    public class User
    {
        [Description( "身份证")]
        [ColumnAttribute(Description = "身份证")]
        public int IdNumber { get; set; }

        [ColumnAttribute(Description = "名称")]
        [Description("名称")]
        public string Name1 { get; set; }

        [ColumnAttribute(Description = "年龄")]
        [Description("年龄")]
        public int Age { get; set; }

        [ColumnAttribute(Description = "性别", Type = ColumnType.CheckBox)]
        [Description("性别")]
        public bool Gender { get; set; }

        [ColumnAttribute(Description = "地址", Type = ColumnType.Link)]
        [Description("地址")]
        public string Address { get; set; }
    }
}
